package heepurlcnnection.zjitc.net.androidhomeworkhttpurlconnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 步骤1：申请权限
 * 步骤2：定义主布局activity_main.xml
 * 步骤3：定义MainActivity
 */
public class MainActivity extends AppCompatActivity {
    ImageView mImageView1, mImageView2, mImageView3, mImageView4;
    Button mButton;
    private String urlStr1 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572192369491&di=e92234d5e57f9026c2dcb7e255b67d18&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201508%2F06%2F20150806163534_C2mS8.jpeg";
    private String urlStr2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572192354285&di=965fb0838814b83f8c750ced3e068b3b&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201601%2F16%2F20160116173125_Qcw23.jpeg";
    private String urlStr3 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572192349214&di=ecfe6d9be3aebbe2473d77288e07becc&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fblog%2F201404%2F15%2F20140415204434_wBRB2.jpeg";
    private String urlStr4 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572192322278&di=6ee4629dc86c8539d724b949e5e81fc7&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Ff1450573c09b0128c64b7c8f94c412d399a1503817779e-GqjnkT_fw658";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        //创建点击事件
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getNetImage(mImageView1, urlStr1);
                getNetImage(mImageView2, urlStr2);
                getNetImage(mImageView3, urlStr3);
                getNetImage(mImageView4, urlStr4);
            }
        });

    }

    private void getNetImage(final ImageView imageView, final String urlStr) {
        //创建子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlStr);
                    HttpURLConnection mHttpURLConnection = (HttpURLConnection) url.openConnection();
                    //设置连接超时时间
                    mHttpURLConnection.setConnectTimeout(15000);
                    //设置读取超时时间
                    mHttpURLConnection.setReadTimeout(15000);
                    //设置请求参数
                    mHttpURLConnection.setRequestMethod("GET");
                    //连接
                    mHttpURLConnection.connect();
                    //得到响流
                    InputStream is = mHttpURLConnection.getInputStream();
                    //调用Bitmap方法  将输入流传递给 BitmapFactory.decodeStream(is) 方法
                    final Bitmap bitmap = BitmapFactory.decodeStream(is);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(bitmap);

                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void init() {
        mImageView1 = findViewById(R.id.imageview1);
        mImageView2 = findViewById(R.id.imageview2);
        mImageView3 = findViewById(R.id.imageview3);
        mImageView4 = findViewById(R.id.imageview4);
        mButton = findViewById(R.id.button);
    }
}
